﻿
$(function(){
	var pathName=window.document.location.pathname;
	var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
	var loginUserId = $("#loginUserId").text();
	
	function load() {
		setTimeout(function() {
			$('#pageCount').on('change', function() {
				var pageCountValue = $('#pageCount').val();
				var nowPageNumberValue = $('#nowPageNumber').text();
				$.ajax({
					url:projectName + "/Word/pageCountChange",
					type:"post",
					async:true,
					data:{
						"pageCount" : pageCountValue,
						"nowPageNumber" : nowPageNumberValue
					},
					dataType:"json",
					success:function (data) {
						$('#dataShow').empty();
						$.ajax({
							url:projectName + "/StarWord/selectAllByLoginUser",
							type:"post",
							async:true,
							data:{
								"loginUserId":loginUserId
							},
							dataType:"json",
							success:function(allStarWord) {
								for(var i = 0; i <data.length; i++) {
									let wordId = data[i].wordId;
									let wordChinese = data[i].wordChinese;
									let wordPing = data[i].wordPing;
									let wordPian = data[i].wordPian;
									let wordWord = data[i].wordWord;
									let wordContend = data[i].wordContend;
									let wordSexName = data[i].wordSex.wordSexName;
									let gradeName = data[i].grade.gradeName;
									if(wordPing == null){
										wordPing = "";
									}
									if(wordPian == null){
										wordPian = "";
									}
									if(wordWord == null){
										wordWord = "";
									}
									if(wordContend == null){
										wordContend = "";
									}
									let $_tr = $('<tr></tr>');
									let $_td1 = $('<td>' + wordChinese + '</td>');
									let $_td2 = $('<td>' + wordPing + '</td>');
									let $_td3 = $('<td>' + wordPian + '</td>');
									let $_td4 = $('<td>' + wordWord + '</td>');
									let $_td5 = $('<td>' + wordContend + '</td>');
									let $_td8 = $('<td>' + wordSexName + '</td>');
									let $_td9 = $('<td>' + gradeName + '</td>');
									$_tr.append($_td1);
									$_tr.append($_td2);
									$_tr.append($_td3);
									$_tr.append($_td4);
									$_tr.append($_td8);
									$_tr.append($_td9);
									$_tr.append($_td5);
									let $_td6 = $('<td class="text-center"></td>');
									var flag = 1;
									for(var j = 0; j <allStarWord.length; j++) {
										if(allStarWord[j].word.wordId == wordId) {
											flag = 0;
										}
									}
									let $_a1 = $('<a href="' + projectName + '/StarWord/add?user.userId=' + loginUserId + '&word.wordId=' + wordId + '"></a>');
									let $_button1 = $('<button class="btn btn-warning">收藏</button>');
									$_a1.append($_button1);
									let $_a2 = $('<a href="' + projectName + '/StarWord/delete?user.userId=' + loginUserId + '&word.wordId=' + wordId + '"></a>');
									let $_button2 = $('<button class="btn btn-danger">取消收藏</button>');
									$_a2.append($_button2);
									if(flag == 0) {
										$_td6.append($_a2);
									} else {
										$_td6.append($_a1);
									}
									$_tr.append($_td6);
									$('#dataShow').append($_tr);
								};
							}
						});
					}
				});
				setTimeout(load(),1);
			});
			
			$('.pageNumber').on('click', function() {
				var pageCountValue = $('#pageCount').val();
				var toPageNumberValue = $(this).text();
				$.ajax({
					url:projectName + "/Word/toPage",
					type:"post",
					async:true,
					data:{
						"pageCount" : pageCountValue,
						"toPageNumber" : toPageNumberValue
					},
					dataType:"json",
					success:function (data) {
						$('#dataShow').empty();
						$.ajax({
							url:projectName + "/StarWord/selectAllByLoginUser",
							type:"post",
							async:true,
							data:{
								"loginUserId":loginUserId
							},
							dataType:"json",
							success:function(allStarWord) {
								for(var i = 0; i <data.length; i++) {
									let wordId = data[i].wordId;
									let wordChinese = data[i].wordChinese;
									let wordPing = data[i].wordPing;
									let wordPian = data[i].wordPian;
									let wordWord = data[i].wordWord;
									let wordContend = data[i].wordContend;
									let wordSexName = data[i].wordSex.wordSexName;
									let gradeName = data[i].grade.gradeName;
									if(wordPing == null){
										wordPing = "";
									}
									if(wordPian == null){
										wordPian = "";
									}
									if(wordWord == null){
										wordWord = "";
									}
									if(wordContend == null){
										wordContend = "";
									}
									let $_tr = $('<tr></tr>');
									let $_td1 = $('<td>' + wordChinese + '</td>');
									let $_td2 = $('<td>' + wordPing + '</td>');
									let $_td3 = $('<td>' + wordPian + '</td>');
									let $_td4 = $('<td>' + wordWord + '</td>');
									let $_td5 = $('<td>' + wordContend + '</td>');
									let $_td8 = $('<td>' + wordSexName + '</td>');
									let $_td9 = $('<td>' + gradeName + '</td>');
									$_tr.append($_td1);
									$_tr.append($_td2);
									$_tr.append($_td3);
									$_tr.append($_td4);
									$_tr.append($_td8);
									$_tr.append($_td9);
									$_tr.append($_td5);
									let $_td6 = $('<td class="text-center"></td>');
									var flag = 1;
									for(var j = 0; j <allStarWord.length; j++) {
										if(allStarWord[j].word.wordId == wordId) {
											flag = 0;
										}
									}
									let $_a1 = $('<a href="' + projectName + '/StarWord/add?user.userId=' + loginUserId + '&word.wordId=' + wordId + '"></a>');
									let $_button1 = $('<button class="btn btn-warning">收藏</button>');
									$_a1.append($_button1);
									let $_a2 = $('<a href="' + projectName + '/StarWord/delete?user.userId=' + loginUserId + '&word.wordId=' + wordId + '"></a>');
									let $_button2 = $('<button class="btn btn-danger">取消收藏</button>');
									$_a2.append($_button2);
									if(flag == 0) {
										$_td6.append($_a2);
									} else {
										$_td6.append($_a1);
									}
									$_tr.append($_td6);
									$('#dataShow').append($_tr);
								};
							}
						});
					}
				});
				setTimeout(load(),1);
			});
			
			$('#prevPage').on('click', function() {
				var pageCountValue = $('#pageCount').val();
				var nowPageNumberValue = $('#nowPageNumber').text();
				var toPageNumberValue = nowPageNumberValue;
				if(toPageNumberValue >= 1) {
					$.ajax({
						url:projectName + "/Word/toPage",
						type:"post",
						async:true,
						data:{
							"pageCount" : pageCountValue,
							"toPageNumber" : toPageNumberValue
						},
						dataType:"json",
						success:function (data) {
							$('#dataShow').empty();
							$.ajax({
								url:projectName + "/StarWord/selectAllByLoginUser",
								type:"post",
								async:true,
								data:{
									"loginUserId":loginUserId
								},
								dataType:"json",
								success:function(allStarWord) {
									for(var i = 0; i <data.length; i++) {
										let wordId = data[i].wordId;
										let wordChinese = data[i].wordChinese;
										let wordPing = data[i].wordPing;
										let wordPian = data[i].wordPian;
										let wordWord = data[i].wordWord;
										let wordContend = data[i].wordContend;
										let wordSexName = data[i].wordSex.wordSexName;
										let gradeName = data[i].grade.gradeName;
										if(wordPing == null){
											wordPing = "";
										}
										if(wordPian == null){
											wordPian = "";
										}
										if(wordWord == null){
											wordWord = "";
										}
										if(wordContend == null){
											wordContend = "";
										}
										let $_tr = $('<tr></tr>');
										let $_td1 = $('<td>' + wordChinese + '</td>');
										let $_td2 = $('<td>' + wordPing + '</td>');
										let $_td3 = $('<td>' + wordPian + '</td>');
										let $_td4 = $('<td>' + wordWord + '</td>');
										let $_td5 = $('<td>' + wordContend + '</td>');
										let $_td8 = $('<td>' + wordSexName + '</td>');
										let $_td9 = $('<td>' + gradeName + '</td>');
										$_tr.append($_td1);
										$_tr.append($_td2);
										$_tr.append($_td3);
										$_tr.append($_td4);
										$_tr.append($_td8);
										$_tr.append($_td9);
										$_tr.append($_td5);
										let $_td6 = $('<td class="text-center"></td>');
										var flag = 1;
										for(var j = 0; j <allStarWord.length; j++) {
											if(allStarWord[j].word.wordId == wordId) {
												flag = 0;
											}
										}
										let $_a1 = $('<a href="' + projectName + '/StarWord/add?user.userId=' + loginUserId + '&word.wordId=' + wordId + '"></a>');
										let $_button1 = $('<button class="btn btn-warning">收藏</button>');
										$_a1.append($_button1);
										let $_a2 = $('<a href="' + projectName + '/StarWord/delete?user.userId=' + loginUserId + '&word.wordId=' + wordId + '"></a>');
										let $_button2 = $('<button class="btn btn-danger">取消收藏</button>');
										$_a2.append($_button2);
										if(flag == 0) {
											$_td6.append($_a2);
										} else {
											$_td6.append($_a1);
										}
										$_tr.append($_td6);
										$('#dataShow').append($_tr);
									};
								}
							});
						}
					});
				}
				setTimeout(load(),1);
			});
			
			$('#nextPage').on('click', function() {
				var pageCountValue = $('#pageCount').val();
				var nowPageNumberValue = $('#nowPageNumber').text();
				var toPageNumberValue = nowPageNumberValue;
				var countNum = $('#count').text();
				var maxPage = Math.ceil(countNum/pageCountValue);
				if(maxPage - toPageNumberValue >= 0) {
					$.ajax({
						url:projectName + "/Word/toPage",
						type:"post",
						async:true,
						data:{
							"pageCount" : pageCountValue,
							"toPageNumber" : toPageNumberValue
						},
						dataType:"json",
						success:function (data) {
							$('#dataShow').empty();
							$.ajax({
								url:projectName + "/StarWord/selectAllByLoginUser",
								type:"post",
								async:true,
								data:{
									"loginUserId":loginUserId
								},
								dataType:"json",
								success:function(allStarWord) {
									for(var i = 0; i <data.length; i++) {
										let wordId = data[i].wordId;
										let wordChinese = data[i].wordChinese;
										let wordPing = data[i].wordPing;
										let wordPian = data[i].wordPian;
										let wordWord = data[i].wordWord;
										let wordContend = data[i].wordContend;
										let wordSexName = data[i].wordSex.wordSexName;
										let gradeName = data[i].grade.gradeName;
										if(wordPing == null){
											wordPing = "";
										}
										if(wordPian == null){
											wordPian = "";
										}
										if(wordWord == null){
											wordWord = "";
										}
										if(wordContend == null){
											wordContend = "";
										}
										let $_tr = $('<tr></tr>');
										let $_td1 = $('<td>' + wordChinese + '</td>');
										let $_td2 = $('<td>' + wordPing + '</td>');
										let $_td3 = $('<td>' + wordPian + '</td>');
										let $_td4 = $('<td>' + wordWord + '</td>');
										let $_td5 = $('<td>' + wordContend + '</td>');
										let $_td8 = $('<td>' + wordSexName + '</td>');
										let $_td9 = $('<td>' + gradeName + '</td>');
										$_tr.append($_td1);
										$_tr.append($_td2);
										$_tr.append($_td3);
										$_tr.append($_td4);
										$_tr.append($_td8);
										$_tr.append($_td9);
										$_tr.append($_td5);
										let $_td6 = $('<td class="text-center"></td>');
										var flag = 1;
										for(var j = 0; j <allStarWord.length; j++) {
											if(allStarWord[j].word.wordId == wordId) {
												flag = 0;
											}
										}
										let $_a1 = $('<a href="' + projectName + '/StarWord/add?user.userId=' + loginUserId + '&word.wordId=' + wordId + '"></a>');
										let $_button1 = $('<button class="btn btn-warning">收藏</button>');
										$_a1.append($_button1);
										let $_a2 = $('<a href="' + projectName + '/StarWord/delete?user.userId=' + loginUserId + '&word.wordId=' + wordId + '"></a>');
										let $_button2 = $('<button class="btn btn-danger">取消收藏</button>');
										$_a2.append($_button2);
										if(flag == 0) {
											$_td6.append($_a2);
										} else {
											$_td6.append($_a1);
										}
										$_tr.append($_td6);
										$('#dataShow').append($_tr);
									};
								}
							});
						}
					});
				}
				setTimeout(load(),1);
			});
			
			$('#goPageButton').on('click', function() {
				var pageCountValue = $('#pageCount').val();
				var toPageNumberValue = $('#goPageInput').val();
				var countNum = $('#count').text();
				var maxPage = Math.ceil(countNum/pageCountValue);
				if(maxPage - toPageNumberValue < 0) {
					toPageNumberValue = maxPage;
				}
				if(toPageNumberValue <= 0) {
					$('#goPageInput').val('1');
					toPageNumberValue = 1;
				}
				$.ajax({
					url:projectName + "/Word/toPage",
					type:"post",
					async:true,
					data:{
						"pageCount" : pageCountValue,
						"toPageNumber" : toPageNumberValue
					},
					dataType:"json",
					success:function (data) {
						$('#dataShow').empty();
						$.ajax({
							url:projectName + "/StarWord/selectAllByLoginUser",
							type:"post",
							async:true,
							data:{
								"loginUserId":loginUserId
							},
							dataType:"json",
							success:function(allStarWord) {
								for(var i = 0; i <data.length; i++) {
									let wordId = data[i].wordId;
									let wordChinese = data[i].wordChinese;
									let wordPing = data[i].wordPing;
									let wordPian = data[i].wordPian;
									let wordWord = data[i].wordWord;
									let wordContend = data[i].wordContend;
									let wordSexName = data[i].wordSex.wordSexName;
									let gradeName = data[i].grade.gradeName;
									if(wordPing == null){
										wordPing = "";
									}
									if(wordPian == null){
										wordPian = "";
									}
									if(wordWord == null){
										wordWord = "";
									}
									if(wordContend == null){
										wordContend = "";
									}
									let $_tr = $('<tr></tr>');
									let $_td1 = $('<td>' + wordChinese + '</td>');
									let $_td2 = $('<td>' + wordPing + '</td>');
									let $_td3 = $('<td>' + wordPian + '</td>');
									let $_td4 = $('<td>' + wordWord + '</td>');
									let $_td5 = $('<td>' + wordContend + '</td>');
									let $_td8 = $('<td>' + wordSexName + '</td>');
									let $_td9 = $('<td>' + gradeName + '</td>');
									$_tr.append($_td1);
									$_tr.append($_td2);
									$_tr.append($_td3);
									$_tr.append($_td4);
									$_tr.append($_td8);
									$_tr.append($_td9);
									$_tr.append($_td5);
									let $_td6 = $('<td class="text-center"></td>');
									var flag = 1;
									for(var j = 0; j <allStarWord.length; j++) {
										if(allStarWord[j].word.wordId == wordId) {
											flag = 0;
										}
									}
									let $_a1 = $('<a href="' + projectName + '/StarWord/add?user.userId=' + loginUserId + '&word.wordId=' + wordId + '"></a>');
									let $_button1 = $('<button class="btn btn-warning">收藏</button>');
									$_a1.append($_button1);
									let $_a2 = $('<a href="' + projectName + '/StarWord/delete?user.userId=' + loginUserId + '&word.wordId=' + wordId + '"></a>');
									let $_button2 = $('<button class="btn btn-danger">取消收藏</button>');
									$_a2.append($_button2);
									if(flag == 0) {
										$_td6.append($_a2);
									} else {
										$_td6.append($_a1);
									}
									$_tr.append($_td6);
									$('#dataShow').append($_tr);
								};
							}
						});
					}
				});
				setTimeout(load(),1);
			});
		},100);
	};
	setTimeout(load(),1);
});